<?php
	class LoginAction extends Action {
	
		public function login(){
			D('Member')->_locallogin(1);
			/*$_SESSION['member'] = D('member')->where('uid = 1')->find();
			$_COOKIE['rcwxuid'] = 1;*/
			//$this->display();
			redirect(U('Index/index'));
		}
		
		public function wxlogin(){
			$uid = authcode(cookie('rcwxuid'),'DECODE');
			if(empty($uid)||empty($_SESSION['member'])){
				if(empty($_GET['code'])){
					$redirect_uri = urlencode('http://www.rencailaile.com'.U('Login/wxlogin'));
					$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('WX_APPID').'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
					header('Location: '.$url);
				}else{
					$access_token = D('Weixin')->getaccess_login($_GET['code']);
					$userinfo = D('Weixin')->getuserinfo_login($access_token['access_token'],$access_token['openid']);
					$result = D('Weixin')->binduser_login($userinfo);
					if($result){
						if(!isset($_SESSION['redirect_uri'])){
							redirect(U('Resume/resume'));
						}else{
							$url = $_SESSION['redirect_uri'];
							redirect(urldecode($url));
						}
					}else{
						redirect(U('Login/wxlogin'));
					}
				}
			}else{
				redirect(U('Resume/resume'));
			}
		}
		
		public function getopenid(){
			$access_token = D('Weixin')->getaccess_login($_GET['code']);
			$userinfo = D('Weixin')->getuserinfo_login($access_token['access_token'],$access_token['openid']);
			$user_info = D('member')->where('wx_id = "'.$userinfo['openid'].'"')->find();
			if(empty($user_info)){
				$member = array(
							'union' => $userinfo['unionid'],
							'wx_id' => $userinfo['openid'],
							'is_active' => 1,
							'ctime' => time(),
							'regip' => '127.0.0.1',
							'lastlogin' => time()
						);
				$user = D('member')->add($member);
				$member_info = array(
								'uid' => $user,
								'ctime' => time()
							);
				$user_info = D('member_info')->add($member_info);
				$openids = array(
								'uid' => $user,
								'openid' => $_GET['openid']
							);
				$openids = D('member_openids')->add($openids);
			}else{
				$openids = array(
								'uid' => $user_info['uid'],
								'openid' => $_GET['openid']
							);
				$openids = D('member_openids')->add($openids);
			}
			$this->display();
		}
		
		public function register(){
			if($_POST['yzcode']==$_SESSION['yzcode'][$_POST['phone']]['code']&&(time()-$_SESSION['yzcode'][$_POST['phone']]['ctime'])<=1800){
				$data['login'] = $_POST['phone'];
				$data['uname'] = $_POST['uname'];
				$data['login_salt'] = rand(10000,99999);
				$data['password'] = md5(md5($_POST['pwd']).$data['login_salt']);
				$data['is_active'] = 1;
				$data['ctime'] = time();
				$data['from'] = 1;
				$data['regip'] = get_client_ip();
				$data['lastlogin'] = time();
				if(!empty($_SESSION['wxid'])){
					$data['wx'] = $_SESSION['wxid'];
				}
				
				if(D('member')->add($data)){
					unset($_SESSION['yzcode'][$_POST['phone']]);
					$return = array('status'=>1,'data'=>'注册成功');
				}else{
					$return = array('status'=>0,'data'=>'注册失败');
				}
				exit(json_encode($return));
			}else{
				exit(json_encode(array('status'=>0,'data'=>'验证码错误或已超时！')));
			}
		}

		public function dologin(){
			$data = $_POST;
			$result = D('Member')->dologin($data);
			if($result){
				$this->success('登陆成功',U('Resume/resume'));
			}else{
				$this->error('登陆失败',U('Login/reg'));
			}
		}
		
		public function reg(){
			$this->display();
		}
		
		
		public function checkphone(){
			$phone = $_POST['phone'];
			$result = D('member')->where(array('login'=>$phone))->count();
			if($result==0){
				$return = array('status'=>1,'data'=>'手机号可以注册');
			}else{
				$return = array('status'=>0,'data'=>'该手机号已经被注册');
			}
			exit(json_encode($return));
		}
		
		public function loginout(){
			unset($_SESSION['member']);
			redirect(U('Login/login'));
		}

		//求职者解绑
		public function no_blind(){
			$uid = $_SESSION['member']['uid'];
			$map['uid'] = $uid;
			$data['wx'] = NULL;
			$da = D('member')->where($map)->save($data);
			if($da){
				$return = array('status'=>1,'data'=>'解除绑定成功');
			}else{
				$return = array('status'=>0,'data'=>'解除绑定失败');
			}
			exit(json_encode($return));
		}
		
		public function no_cookie(){
			unset($_SESSION['member']);
			cookie('rcwxuid',null);
		}

		public function yzcode(){
			if((time()-$_SESSION['yzcode'][$_POST['phone']]['ctime'])>1800){
				$_SESSION['yzcode'][$_POST['phone']] = array('code' => rand(100000,999999),'ctime'=>time());
			}
			$msg = '欢迎注册海营之家，您的注册验证码：'.$_SESSION['yzcode'][$_POST['phone']]['code'].'。【海营之家】';
			$result = $this->SendMesg($msg,$_POST['phone']);
			if($result){
				$return = array('status'=>1,'data'=>'短信发送成功');
			}else{
				$return = array('status'=>0,'data'=>'短信发送失败，请重新确认手机号码');
			}
			exit(json_encode($return));
		}
		
		 /*发送短信的接口
	 */
	public function SendMesg($con,$num){
		require_once 'HttpClient.class.php';
		$client = new HttpClient ("www.lx198.com");
		if (! $client->post ( '/sdk/send', array (
				'accName' => "1152405@qq.com",
				'accPwd' => strtoupper ( md5 ( "dd200412" ) ),
				'bizId' => date ( 'YmdHis' ),
				'content' => mb_convert_encoding ( $con, 'UTF-8', 'UTF-8' ),
				'aimcodes' =>$num ,
				'dataType'=>"xml"
		) )) {
			return false;
		} else {
			$xmlStr = $client->getContent () ;
			$doc = new DOMDocument ();
			$doc->loadXML ( $xmlStr );
			$xml = simplexml_load_string($xmlStr);
			if($xml->replyCode == 1){
				return true;
			}else{
				return false;
			}
		}
	}
	
	}
?>